package com.hyz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hyz.model.IsComplete;
import com.hyz.model.ResponseResult;
import com.hyz.model.asset.Asset;
import com.hyz.model.asset.AssetStatus;
import com.hyz.model.assetImport.AssetImport;
import com.hyz.model.assetImport.AssetImportResult;
import com.hyz.model.assetOutbound.AssetOutbound;
import com.hyz.model.assetOutbound.AssetOutboundResult;
import com.hyz.model.repositoryCharge.RepositoryCharge;
import com.hyz.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author HGD
 * @date 2022/10/8 13:01
 */
@Api("仓管")
@RequestMapping("/repoManager")
@RestController
public class RepoManagerController {
    @Autowired
    private RepositoryChargeService rcService;
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private AssetImportService aiService;
    @Autowired
    private AssetService assetService;
    @Autowired
    private AssetOutboundService aoService;

    //----------------------------------------------------------------
    // 资产入库


    @ApiOperation("仓管获取入库申请")
    @PostMapping("/getAssetImports")
    public ResponseResult<List<AssetImport>> getAssetImports(@RequestBody Long repoManagerId) {
        List<AssetImport> assetImports = new ArrayList<>();
        // 1. 搜索是否对应的监管仓库
        QueryWrapper<RepositoryCharge> rcWrapper = new QueryWrapper<>();
        rcWrapper.lambda().eq(RepositoryCharge::getRepoManagerId, repoManagerId);
        List<RepositoryCharge> charges = rcService.list(rcWrapper);
        if (Objects.isNull(charges)) {
            return new ResponseResult<>(400, "没有对应的监管仓库");
        }
        for (RepositoryCharge charge : charges) {
            Long repositoryId = charge.getRepositoryId();
            // 2. 判断仓库是否存在
            if (Objects.isNull(repositoryService.getById(repositoryId))) {
                return new ResponseResult<>(400, "监管仓库:" + repositoryId + " 已不存在，请联系管理人员");
            }
            // 3. 根据仓库id搜索未完成待审批的入库申请列表
            QueryWrapper<AssetImport> aiWrapper = new QueryWrapper<>();
            aiWrapper.lambda().eq(AssetImport::getRepositoryId, repositoryId)
                    .eq(AssetImport::getResult, AssetImportResult.CREATE)
                    .eq(AssetImport::getIsComplete, IsComplete.UNFINISHED);
            List<AssetImport> list = aiService.list(aiWrapper);
            assetImports.addAll(list);
        }
        if (assetImports.isEmpty()) {
            return new ResponseResult<>(400, "无可入库申请");
        }
        return new ResponseResult<>(200, "获取入库申请", assetImports);
    }

    @ApiOperation("仓管审批入库申请")
    @PostMapping("/updateAssetImport")
    public ResponseResult<String> updateAssetImport(@RequestParam("assetImportId") Long assetImportId,
                                                    @RequestParam("result") AssetImportResult result) {
        // 1. 判断入库申请是否存在
        AssetImport assetImport = aiService.getById(assetImportId);
        if (Objects.isNull(assetImport)) {
            return new ResponseResult<>(400, "入库申请不存在");
        }
        // 2. 判断入库申请状态是否完结
        if (assetImport.getResult() != AssetImportResult.CREATE || assetImport.getIsComplete() != IsComplete.UNFINISHED) {
            return new ResponseResult<>(400, "该入库申请已完成，请勿重复提交");
        }
        // 3. 修改入库申请状态，完结状态
        assetImport.setResult(result);
        assetImport.setIsComplete(IsComplete.COMPLETE);

        // 4. 修改入库申请对应的资产状态
        if (!Objects.equals(result, AssetImportResult.FAILED)) {
            Long assetId = assetImport.getAssetId();
            UpdateWrapper<Asset> assetWrapper = new UpdateWrapper<>();
            assetWrapper.lambda().eq(Asset::getId, assetId)
                    .set(Asset::getStatus, AssetStatus.IN_REPOSITORY);
            assetService.update(assetWrapper);
        }
        if (aiService.saveOrUpdate(assetImport)) {
            return new ResponseResult<>(200, "入库成功");
        }
        return new ResponseResult<>(400, "入库失败，请稍后再试");
    }

    //------------------------------------------------------------------------------------------------
    // 资产出库

    @ApiOperation("仓管获取出库申请")
    @PostMapping("/rmGetAssetOutbounds")
    public ResponseResult<List<AssetOutbound>> rmGetAssetOutbounds(@RequestParam Long repoManagerId) {

        // 1. 判断仓管是否有负责的仓库
        QueryWrapper<RepositoryCharge> rcWrapper = new QueryWrapper<>();
        rcWrapper.lambda().eq(RepositoryCharge::getRepoManagerId, repoManagerId);
        List<RepositoryCharge> charges = rcService.list(rcWrapper);
        if (charges.isEmpty()) {
            return new ResponseResult<>(400, "没有对应的监管仓库");
        }
        List<AssetOutbound> assetOutbounds = new ArrayList<>();
        for (RepositoryCharge charge : charges) {
            Long repositoryId = charge.getRepositoryId();
            // 2. 判断仓库是否存在
            if (Objects.isNull(repositoryService.getById(repositoryId))) {
                return new ResponseResult<>(400, "监管仓库:" + repositoryId + " 已不存在，请联系管理人员");
            }
            // 3. 根据仓库id搜索未完成待审批的入库申请列表
            QueryWrapper<AssetOutbound> aiWrapper = new QueryWrapper<>();
            aiWrapper.lambda().eq(AssetOutbound::getRepositoryId, repositoryId)
                    .eq(AssetOutbound::getResult, AssetImportResult.CREATE)
                    .eq(AssetOutbound::getIsComplete, IsComplete.UNFINISHED);
            assetOutbounds.addAll(aoService.list(aiWrapper));
        }
        if (assetOutbounds.isEmpty()) {
            return new ResponseResult<>(400, "无可出库申请");
        }
        return new ResponseResult<>(200, "获取出库申请", assetOutbounds);
    }

    @ApiOperation("仓管审批出库申请")
    @PostMapping("/rmUpdateAssetOutbound")
    public ResponseResult<String> rmUpdateAssetOutbound(@RequestParam("id") Long id,
                                                        @RequestParam("result") AssetOutboundResult result) {
        // 1. 判断出库申请是否存在
        AssetOutbound assetOutbound = aoService.getById(id);
        if (Objects.isNull(assetOutbound)) {
            return new ResponseResult<>(400, "出库申请不存在");
        }
        // 2. 判断出库申请是否完成
        if (assetOutbound.getIsComplete() != IsComplete.UNFINISHED) {
            return new ResponseResult<>(400, "出库申请已完成");
        }
        // 3. 判断出库申请状态
        if (assetOutbound.getResult() != AssetOutboundResult.CREATE) {
            return new ResponseResult<>(400, "此处出库申请状态有问题");
        }

        // 4. 判断资产是否存在
        Asset asset = assetService.getById(assetOutbound.getAssetId());
        if (Objects.isNull(asset)) {
            return new ResponseResult<>(400, "资产不存在");
        }
        // 5. 判断资产状态是否正确
        if (asset.getStatus() != AssetStatus.LOCK) {
            return new ResponseResult<>(400, "资产已被借调");
        }
        // 6. 资产状态更新
        if (Objects.equals(result, AssetOutboundResult.FAILED)) {
            asset.setStatus(AssetStatus.IN_REPOSITORY);
            assetService.saveOrUpdate(asset);
        }
        // 7. 修改出库申请状态
        assetOutbound.setResult(result);
        assetOutbound.setIsComplete(IsComplete.COMPLETE);
        if (aoService.saveOrUpdate(assetOutbound)) {
            return new ResponseResult<>(200, "审批成功");
        }
        return new ResponseResult<>(400, "审批失败");
    }
}
